package com.so206.controller;

import com.so206.crond.process.VerfySign;
import com.so206.interceptr.BaseInterceptor;
import com.so206.plugins.pay.YiPayFunction;
import com.so206.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 错误处理单元
 */
@ControllerAdvice
public class SysErrorController {

    @Autowired
    private YiPayFunction yiPayFunction;

    @ExceptionHandler(value = Exception.class)
    public String defaultErrorHandler(HttpServletRequest req, Exception e, Model model) {
        e.printStackTrace();
        model.addAttribute("error_msg", e.toString());
        model.addAttribute("trace", e.getStackTrace());
        if (!BaseInterceptor.verify) {
            model.addAttribute("error_list", VerfySign.error_file);
        } else {
            Map map = HttpClientUtils.getParameterMap(req);
            String string = yiPayFunction.createLinkString(map);
            String[] split = string.split("&");
            model.addAttribute("para", split);
            model.addAttribute("url", req.getRequestURL());
        }
        model.addAttribute("verify", BaseInterceptor.verify);
        return "500";
    }


}
